package com.hh.bidding.service.business;

import com.hh.bidding.domain.BidNotice;
import com.hh.bidding.domain.BidTenderInvitation;
import com.hh.bidding.service.IBidNoticeService;
import com.hh.bidding.service.IBidTenderInvitationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class BidInvitationServiceBusiness {
    @Resource
    private IBidNoticeService bidNoticeService;
    @Resource
    private IBidTenderInvitationService bidTenderInvitationService;
    @Resource
    private IBidMailService bidMailService;

    public List<BidTenderInvitation> handlerInvitation(Long uid,Long sid,String sCode, List<Long> list){
        BidTenderInvitation tempBidTenderInvitation = new BidTenderInvitation();
        tempBidTenderInvitation.setUid(uid);
        tempBidTenderInvitation.setSid(sid);
        List<BidTenderInvitation> oldList = bidTenderInvitationService.selectBidTenderInvitationList(tempBidTenderInvitation);
        List<BidTenderInvitation> newList = new ArrayList<>();
        List<BidTenderInvitation> newSuccessList = new ArrayList<>();
        List<BidTenderInvitation> deleteList = new ArrayList<>();
        if(oldList == null || oldList.size() == 0){
            //全部直接插入
            for(Long hid : list){
                BidTenderInvitation bidTenderInvitation = new BidTenderInvitation();
                bidTenderInvitation.setSid(sid);
                bidTenderInvitation.setsCode(sCode);
                bidTenderInvitation.setHid(hid);
                bidTenderInvitation.setUid(uid);
                bidTenderInvitation.setCreateTime(new Date());
                newList.add(bidTenderInvitation);
            }
        }else{
            //排除
            Map<Long, List<BidTenderInvitation>> map = oldList.stream().collect(Collectors.groupingBy(BidTenderInvitation::getHid));
            for(Long hid : list){
                if(hid != null && hid > 0){
                    List<BidTenderInvitation> tempList = map.get(hid);
                    if(tempList == null){
                        BidTenderInvitation bidTenderInvitation = new BidTenderInvitation();
                        bidTenderInvitation.setSid(sid);
                        bidTenderInvitation.setsCode(sCode);
                        bidTenderInvitation.setHid(hid);
                        bidTenderInvitation.setUid(uid);
                        bidTenderInvitation.setCreateTime(new Date());
                        newList.add(bidTenderInvitation);
                    }
                }
            }
            for(BidTenderInvitation oldInvitation: oldList){
                Long tempHid = oldInvitation.getHid();
                if(tempHid != null && tempHid > 0 && !list.contains(tempHid)){
                    deleteList.add(oldInvitation);
                }
            }
        }
        if(newList.size() > 0){
            for(BidTenderInvitation newInvitation: newList){
                try {
                    //新增
                    bidTenderInvitationService.insertBidTenderInvitation(newInvitation);
                    newSuccessList.add(newInvitation);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        if(deleteList.size() > 0){
            for(BidTenderInvitation deleteInvitation: deleteList){
                try {
                    //新增
                    bidTenderInvitationService.deleteBidTenderInvitationById(deleteInvitation.getId());
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        return newSuccessList;
    }
}
